﻿using SortingSpace;
using System.CodeDom.Compiler;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Windows.Markup;

const string CONSOLE_TEXT = "G - generate new array, T - start test, S - show array, N - exit";
Random rand = new();
int[] nums = null;
Stopwatch sw = new Stopwatch();

#region ref
//var ins = new insertSort(nums);
//ins.Sort();

//foreach (var item in ins.getValues())
//{
//    Console.Write(item);
//    Console.Write(' ');
//}

//Console.WriteLine();

////foreach (var item in nums) Console.Write(item.ToString() + ' ');
////Console.WriteLine();

//var exch = new exchangeSort(nums);
//exch.Sort();

//foreach (var item in exch.getValues())
//{
//    Console.Write(item);
//    Console.Write(' ');
//}

//Console.WriteLine();

////foreach (var item in nums) Console.Write(item.ToString() + ' ');
////Console.WriteLine();

//var sel = new selectionSort(nums);
//sel.Sort();

//foreach (var item in sel.getValues())
//{
//    Console.Write(item);
//    Console.Write(' ');
//}
#endregion

void genArray(int len)
{
    nums = new int[len];
    for (int i = 0; i < len; i++) nums[i] = rand.Next(10000);
}

void test()
{
    show();
    Console.WriteLine();

    Sorting[] sorts = [new insertSort(nums), new exchangeSort(nums), new selectionSort(nums), new mergeSort(nums), new simpleInsertSort(nums)];

    foreach (var sort in sorts)
    {
        sw.Start();
        sort.Sort();
        sw.Stop();

        TimeSpan ts = sw.Elapsed;

        Console.Write("{0, -40}", string.Format("{0}({1:00}:{2:00}:{3:00}:{4:00}):", sort.getName(), ts.Minutes, ts.Seconds, ts.Milliseconds/10, ts.Microseconds/10));

        var sortValues = sort.getValues();
        for (int i = 1; i <= sortValues.Length; i++)
        {
            Console.Write(sortValues[i-1].ToString() + ' ');
            if (i % 10 == 0) Console.Write("\n{0, -40}", "");
        }
        Console.WriteLine();
    }
}

void show()
{
    Console.Write("{0, -40}", "Array: ");
    for (int i = 1; i <= nums.Length ; i++)
    {
        Console.Write(nums[i-1].ToString() + ' ');
        if (i % 10 == 0) Console.Write("\n{0, -40}", "");

    }
}

string input = " ";

while (input != "N")
{
    Console.Clear();
    Console.WriteLine(CONSOLE_TEXT);
    input = Console.ReadLine().ToUpper();

    switch (input)
    {
        case "G":
            Console.Write("Input length: ");
            int count = Convert.ToInt32(Console.ReadLine());
            if (count > 0)
            { 
                genArray(count);
                Console.WriteLine("Cuccess!");
            }
            else Console.WriteLine("length must be more than 0!");
            break;
        case "T":
            if (nums != null)
                test();
            else Console.WriteLine("Array is empty!");
            break;
        case "S":
            if (nums != null)
            {
                Console.WriteLine();
                show();
                Console.WriteLine();
            }
            else Console.WriteLine("Array is empty!");
            break;
    }

    if (input != "N")
        Console.ReadLine();
}